package plugins.schoolcard;

import kd.bos.base.AbstractBasePlugIn;
import kd.bos.dataentity.OperateOption;
import kd.bos.entity.operate.OperateOptionConst;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.control.events.BeforeItemClickEvent;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.print.core.plugin.event.AfterLoadDataEvent;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;

/**
 * 基础资料插件
 */
public class statusLock extends AbstractBasePlugIn implements Plugin {
    // 字段标识常量
    private static final String STATUS_FIELD = "dcb4_status";
    private static final String SUBMIT_BUTTON = "bar_submit";
    private static final String CHECKIN_BUTTON = "dcb4_CheckIn";
    private static final String LOCK_BUTTON = "dcb4_lock";
    private static final String UNLOCK_BUTTON = "dcb4_unlock";
    private static final String DOCUMENT_STATUS = "status"; // 系统状态字段

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        this.getModel().setValue(STATUS_FIELD, "A");
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 注册工具栏按钮点击监听
        this.addItemClickListeners(SUBMIT_BUTTON, LOCK_BUTTON, UNLOCK_BUTTON, CHECKIN_BUTTON);
    }

    @Override
    public void beforeItemClick(BeforeItemClickEvent e) {
        super.beforeItemClick(e);
        String itemKey = e.getItemKey();
        String currentStatus = (String) this.getModel().getValue(STATUS_FIELD);

        // 提交按钮逻辑
        if (SUBMIT_BUTTON.equals(itemKey)) {
            if (!"A".equals(currentStatus)) {
                e.setCancel(true);
            }
        }
        if (CHECKIN_BUTTON.equals(itemKey)) {
            if (!"A".equals(currentStatus)) {
                e.setCancel(true);
            }
        }

        // 锁定按钮逻辑
        if (LOCK_BUTTON.equals(itemKey)) {
            if (!"B".equals(currentStatus)) {
                this.getView().showErrorNotification("只能在状态为B时锁定");
                e.setCancel(true);
            }
        }

        // 解锁按钮逻辑
        if (UNLOCK_BUTTON.equals(itemKey)) {
            if (!"C".equals(currentStatus)) {
                this.getView().showErrorNotification("只能在状态为C时解锁");
                e.setCancel(true);
            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent e) {
        super.itemClick(e);
        String itemKey = e.getItemKey();

        // 创建操作选项并禁用所有提示
        OperateOption option = OperateOption.create();
        option.setVariableValue(OperateOptionConst.ISSHOWMESSAGE, "false");

        // 提交按钮逻辑
        if (SUBMIT_BUTTON.equals(itemKey)) {
            this.getModel().setValue(STATUS_FIELD, "B");
            this.getView().invokeOperation("submit", option);
            this.getView().invokeOperation("audit", option); // 提交后立即审核
        }
        if (CHECKIN_BUTTON.equals(itemKey)) {
            this.getModel().setValue(STATUS_FIELD, "B");
            this.getView().invokeOperation("submit", option);
            this.getView().invokeOperation("audit", option); // 提交后立即审核
        }

        // 锁定按钮逻辑
        if (LOCK_BUTTON.equals(itemKey)) {
            // 如果当前单据状态是已审核，则先进行反审核
            String docStatus = (String) this.getModel().getValue(DOCUMENT_STATUS);
            if ("C".equals(docStatus)) {
                this.getView().invokeOperation("unaudit", option);
            }

            this.getModel().setValue(STATUS_FIELD, "C");
            this.getView().invokeOperation("save", option);
            this.getView().invokeOperation("submit", option);
            this.getView().invokeOperation("audit", option); // 提交后立即审核

            this.getView().showMessage("校园卡账户已锁定");
        }

        // 解锁按钮逻辑
        if (UNLOCK_BUTTON.equals(itemKey)) {
            String docStatus = (String) this.getModel().getValue(DOCUMENT_STATUS);
            if ("C".equals(docStatus)) {
                this.getView().invokeOperation("unaudit", option);
            }

            this.getModel().setValue(STATUS_FIELD, "B");
            this.getView().invokeOperation("save", option);
            this.getView().invokeOperation("submit", option);
            this.getView().invokeOperation("audit", option); // 提交后立即审核

            this.getView().showMessage("校园卡账户已激活");
        }
    }
}

/*
public class statusLock extends AbstractBasePlugIn implements Plugin {
    // 字段标识常量
    private static final String STATUS_FIELD = "dcb4_status";
    private static final String SUBMIT_BUTTON = "bar_submit";
    private static final String LOCK_BUTTON = "dcb4_lock";
    private static final String UNLOCK_BUTTON = "dcb4_unlock"; // 新增解锁按钮标识

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        this.getModel().setValue(STATUS_FIELD, "A");
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        // 注册工具栏按钮点击监听（新增解锁按钮）
        this.addItemClickListeners(SUBMIT_BUTTON, LOCK_BUTTON, UNLOCK_BUTTON);
    }

    @Override
    public void beforeItemClick(BeforeItemClickEvent e) {
        super.beforeItemClick(e);
        String itemKey = e.getItemKey();
        String currentStatus = (String) this.getModel().getValue(STATUS_FIELD);

        // 提交按钮逻辑
        if (SUBMIT_BUTTON.equals(itemKey)) {
            if (!"A".equals(currentStatus)) {
                e.setCancel(true);
            }
        }

        // 锁定按钮逻辑
        if (LOCK_BUTTON.equals(itemKey)) {
            if (!"B".equals(currentStatus)) {
                this.getView().showErrorNotification("只能在状态为B时锁定");
                e.setCancel(true);
            }
        }

        // 解锁按钮逻辑（新增）
        if (UNLOCK_BUTTON.equals(itemKey)) {
            if (!"C".equals(currentStatus)) {
                e.setCancel(true);
            }
        }
    }

    @Override
    public void itemClick(ItemClickEvent e) {
        super.itemClick(e);
        String itemKey = e.getItemKey();

        // 提交按钮逻辑
        if (SUBMIT_BUTTON.equals(itemKey)) {
            this.getModel().setValue(STATUS_FIELD, "B");
            this.getView().showSuccessNotification("账户已激活");

        }

        // 锁定按钮逻辑
        if (LOCK_BUTTON.equals(itemKey)) {
            this.getModel().setValue(STATUS_FIELD, "C");
            this.getView().showSuccessNotification("挂失成功，账户已锁定");

        }

        // 解锁按钮逻辑（新增）
        if (UNLOCK_BUTTON.equals(itemKey)) {
            this.getModel().setValue(STATUS_FIELD, "B");
            this.getView().showSuccessNotification("已激活，账户可使用");

        }
    }
}
 */

